import {onMounted, reactive, watch} from "vue"

export default (baseState, refreshFn) => {
  const state = reactive({
    pageNo: 1,
    pageSize: 20,
    total: 0,
    list: null,
    tableSelections: [],
    tableSelectionIds: null,
  })

  watch(() => [state.pageNo, state.pageSize], () => refreshFn())
  onMounted(refreshFn)

  const tableSelectionChange = (value) => {
    state.tableSelections = value
    state.tableSelectionIds = value.map(v => v.id).join(',') || null
  }

  return {
    listState: state,
    tableSelectionChange,
  }
}
